package com.javalong.affairs.activity;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.constant.BundleKey;
import com.javalong.affairs.constant.DataType;
import com.javalong.affairs.constant.RequestKey;
import com.javalong.affairs.listener.BaseResponseListener;
import com.javalong.affairs.manager.IntentManager;
import com.javalong.affairs.model.MessageInfo;
import com.javalong.affairs.ui.TopTitle;
import com.javalong.affairs.utils.HttpUtils;
import com.javalong.affairs.utils.TimeUtils;
import com.javalong.affairs.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_watchsendmsg)
public class WatchSendMsgActivity extends BaseActivity {
	@ViewInject(R.id.tv_time)
	private TextView tv_time;
	@ViewInject(R.id.tv_address)
	private TextView tv_address;
	@ViewInject(R.id.tv_sender)
	private TextView tv_sender;
	@ViewInject(R.id.tv_receiver)
	private TextView tv_receiver;
	@ViewInject(R.id.tv_status)
	private TextView tv_status;
	@ViewInject(R.id.tv_content)
	private TextView tv_content;
	@ViewInject(R.id.tv_timelimit)
	private TextView tv_timelimit;
	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.tv_title_content)
	private TextView tv_title;
	@ViewInject(R.id.bt_update)
	private Button bt_update;
	@ViewInject(R.id.bt_search)
	private Button bt_search;
	private String msgId;
	private String status;
	private boolean isReceiver;
	private String simpleMsgId;
	private MessageInfo msgInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		Bundle bundle = getIntent().getBundleExtra(BundleKey.BUNDLE);
		msgId = bundle.getString(BundleKey.MSGID);
		simpleMsgId = bundle.getString(BundleKey.SIMPLEMSGID);
		status = bundle.getString(BundleKey.SIMPLEMSGSTATUS);
		isReceiver = bundle.getBoolean(BundleKey.ISRECEIVER);
		initData();
	}

	private void initData() {
		String url = UrlUtils.getUrl("SendMessage", "outOne_messageinfo");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(RequestKey.ID, simpleMsgId);
		HttpUtils.post(url, params, new BaseResponseListener(DataType.MESSAGE) {
			@Override
			public void onSuccess(Object obj) {
				super.onSuccess(obj);
				initUI((MessageInfo) obj);
			}
		});
	}

	private void initUI(MessageInfo msgInfo) {
		this.msgInfo = msgInfo;
		if (msgInfo != null) {
			tv_time.setText(TimeUtils.getFormatTime(msgInfo.time));
			tv_address.setText(msgInfo.place);
			tv_sender.setText(msgInfo.sender);
			tv_receiver.setText(msgInfo.recipient);
			tv_content.setText(msgInfo.messageData);
			tv_timelimit.setText(msgInfo.timeLimit);
			tv_title.setText(msgInfo.title);
		}
		topTitle.setOnLeftDrawableClick(this);
		bt_update.setOnClickListener(this);
		bt_search.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		Bundle bundle = new Bundle();
		switch (v.getId()) {
		case R.id.bt_update:
			bundle.putString(BundleKey.SIMPLEMSGID, simpleMsgId);
			IntentManager.doActivity(this, UpdateMsgActivity.class, bundle);
			break;
		case R.id.bt_search:
			bundle.putString(BundleKey.SIMPLEMSGID, simpleMsgId);
			IntentManager.doActivity(this, MsgStatisticActivity.class, bundle);
			break;
		default:
			break;
		}
	}
}
